package com.cwj.newyear;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView tvDay, tvWeek, tvNongli, tvChuxiDay, tvChuxiHour, tvChunjie, tvChunjieWeek,
            tvChunjieNongli, tvJieri1, tvJieri2, tvJieri3, tvJieri4, tvJu, tvLi, tvChu, tvXi, tvHai, tvYou;
    private Lunar lunar;
    private ImageView ivShengxiao;
    private long nowTime;
    private long chunjie;
    private final List<Jieri> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
//            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
//            return insets;
//        });

        initView();
        initData();
    }

    private void initView() {
        tvDay = findViewById(R.id.tvDay);
        tvWeek = findViewById(R.id.tvWeek);
        tvNongli = findViewById(R.id.tvNongli);
        tvChuxiDay = findViewById(R.id.tvChuxiDay);
        tvChuxiHour = findViewById(R.id.tvChuxiHour);
        tvChunjie = findViewById(R.id.tvChunjie);
        tvChunjieWeek = findViewById(R.id.tvChunjieWeek);
        tvChunjieNongli = findViewById(R.id.tvChunjieNongli);
        ivShengxiao = findViewById(R.id.ivShengxiao);
        tvJieri1 = findViewById(R.id.tvJieri1);
        tvJieri2 = findViewById(R.id.tvJieri2);
        tvJieri3 = findViewById(R.id.tvJieri3);
        tvJieri4 = findViewById(R.id.tvJieri4);
        tvJu = findViewById(R.id.tvJu);
        tvLi = findViewById(R.id.tvLi);
        tvChu = findViewById(R.id.tvChu);
        tvXi = findViewById(R.id.tvXi);
        tvHai = findViewById(R.id.tvHai);
        tvYou = findViewById(R.id.tvYou);
        FallingView fallingView = findViewById(R.id.fallingView);

        //初始化一个雪花样式的fallObject
        FallObject.Builder builder = new FallObject.Builder(getResources().getDrawable(R.drawable.xuehua));
        FallObject fallObject = builder
                .setSpeed(5, true)
                .setSize(30, 30, true)
                .setWind(5, true, true)
                .build();
        fallingView.addFallObject(fallObject, 150);//添加下落物体对象
    }

    @SuppressLint("SetTextI18n")
    private void initData() {
        update();//显示数据
        //开始计时
        handler.postDelayed(runnable, 1000);
        //停止计时
        //handler.removeCallbacks(runnable);
    }

    public static Handler handler = new Handler(Looper.getMainLooper());

    public Runnable runnable = new Runnable() {
        @RequiresApi(api = Build.VERSION_CODES.O)
        public void run() {
            update();//获取新数据
            handler.postDelayed(this, 1000); //n秒刷新一次
        }
    };

    @SuppressLint("SetTextI18n")
    private void update() {
        shuju();
        //今天日期
        tvDay.setText("今天是" + TimeUtils.dateToString(TimeUtils.getTimeStame(), "yyyy年MM月dd日"));
        //星期几
        tvWeek.setText("星期" + lunar.getWeekInChinese());
        //农历
        tvNongli.setText(lunar.getYearInGanZhi() + "(" + lunar.getYearShengXiao() + ")年" + lunar.getMonthInChinese() + "月" + lunar.getDayInChinese());
        //距离除夕还剩多少天
        try {
            long chuxiDay = (Long.parseLong(TimeUtils.dateToStamp(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).next(-1).getSolar().toString() + " 00:00:00")) - nowTime) / (1000 * 60 * 60 * 24);
            long chuxiHoue = ((Long.parseLong(TimeUtils.dateToStamp(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).next(-1).getSolar().toString() + " 00:00:00")) - nowTime) / (1000 * 60 * 60)) % 24;//小时
            long chuxiMinute = ((Long.parseLong(TimeUtils.dateToStamp(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).next(-1).getSolar().toString() + " 00:00:00")) - nowTime) / (1000 * 60)) % 60;//分钟
            long chuxiSecond = ((Long.parseLong(TimeUtils.dateToStamp(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).next(-1).getSolar().toString() + " 00:00:00")) - nowTime) / 1000) % 60;//秒
            if (chunjie - nowTime >= -1000 * 60 * 60 * 24 && chunjie - nowTime <= 0) {
                tvJu.setText("今");
                tvLi.setText("天");
                tvChu.setText("是");
                tvXi.setText("春");
                tvHai.setText("节");
                tvYou.setText("!");
                tvChuxiDay.setText("新年快乐");
                tvChuxiHour.setText("万事如意");
            } else if ((Long.parseLong(TimeUtils.dateToStamp(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).next(-1).getSolar().toString() + " 00:00:00")) - nowTime) > 0) {
                tvJu.setText("距");
                tvLi.setText("离");
                tvChu.setText("除");
                tvXi.setText("夕");
                tvHai.setText("还");
                tvYou.setText("有");
                tvChuxiDay.setText(chuxiDay + "天");
                tvChuxiHour.setText(chuxiHoue + "小时" + chuxiMinute + "分" + chuxiSecond + "秒");
            } else {
                tvJu.setText("今");
                tvLi.setText("天");
                tvChu.setText("是");
                tvXi.setText("除");
                tvHai.setText("夕");
                tvYou.setText("!");
                tvChuxiDay.setText("新年快乐");
                tvChuxiHour.setText("万事如意");
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //下个春节时间
        tvChunjie.setText("春节时间：" + Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getSolar().toString());
        //下个春节当天星期几
        tvChunjieWeek.setText("星期" + Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getSolar().getWeekInChinese());
        //下个春节农历及生肖
        tvChunjieNongli.setText(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearInGanZhi() + "(" + Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao() + ")" + "年");
        //下个春节生肖图片显示
        if ("鼠".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.shu);
        } else if ("牛".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.niu);
        } else if ("虎".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.hu);
        } else if ("兔".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.tu);
        } else if ("龙".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.dalong);
        } else if ("蛇".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.she);
        } else if ("马".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.ma);
        } else if ("羊".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.yang);
        } else if ("猴".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.hou);
        } else if ("鸡".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.ji);
        } else if ("狗".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.gou);
        } else if ("猪".equals(Lunar.fromYmd(lunar.getYear() + 1, 1, 1).getYearShengXiao())) {
            ivShengxiao.setImageResource(R.drawable.zhu);
        }

        tvJieri1.setText("");
        tvJieri2.setText("");
        tvJieri3.setText("");
        tvJieri4.setText("");
        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                if (i == 0) {
                    //节日1
                    if (list.get(i).getTime() > -1000 * 60 * 60 * 24 && list.get(i).getTime() <= 0) {
                        tvJieri1.setText("今天：" + list.get(i).name);
                    } else if (list.get(i).getTime() > 0 && list.get(i).getTime() <= 1000 * 60 * 60 * 24) {
                        tvJieri1.setText("明天：" + list.get(i).name);
                    } else if (list.get(i).getTime() > 1000 * 60 * 60 * 24) {
                        tvJieri1.setText(list.get(i).getTime() / (1000 * 60 * 60 * 24) + "天后：" + list.get(i).name);
                    }
                } else if (i == 1) {
                    //节日2
                    if (list.get(i).getTime() > -1000 * 60 * 60 * 24 && list.get(i).getTime() <= 0) {
                        tvJieri2.setText("今天：" + list.get(i).name);
                    } else if (list.get(i).getTime() > 0 && list.get(i).getTime() <= 1000 * 60 * 60 * 24) {
                        tvJieri2.setText("明天：" + list.get(i).name);
                    } else if (list.get(i).getTime() > 1000 * 60 * 60 * 24) {
                        tvJieri2.setText(list.get(i).getTime() / (1000 * 60 * 60 * 24) + "天后：" + list.get(i).name);
                    }
                } else if (i == 2) {
                    //节日3
                    if (list.get(i).getTime() > -1000 * 60 * 60 * 24 && list.get(i).getTime() <= 0) {
                        tvJieri3.setText("今天：" + list.get(i).name);
                    } else if (list.get(i).getTime() > 0 && list.get(i).getTime() <= 1000 * 60 * 60 * 24) {
                        tvJieri3.setText("明天：" + list.get(i).name);
                    } else if (list.get(i).getTime() > 1000 * 60 * 60 * 24) {
                        tvJieri3.setText(list.get(i).getTime() / (1000 * 60 * 60 * 24) + "天后：" + list.get(i).name);
                    }
                } else if (i == 3) {
                    //节日4
                    if (list.get(i).getTime() > -1000 * 60 * 60 * 24 && list.get(i).getTime() <= 0) {
                        tvJieri4.setText("今天：" + list.get(i).name);
                    } else if (list.get(i).getTime() > 0 && list.get(i).getTime() <= 1000 * 60 * 60 * 24) {
                        tvJieri4.setText("明天：" + list.get(i).name);
                    } else if (list.get(i).getTime() > 1000 * 60 * 60 * 24) {
                        tvJieri4.setText(list.get(i).getTime() / (1000 * 60 * 60 * 24) + "天后：" + list.get(i).name);
                    }
                }
            }
        }
    }

    private void shuju() {
        //当前时间
        lunar = Lunar.fromDate(new Date());
        nowTime = TimeUtils.getTimeStame();
        long nowTimeYMD;
        String nowTimeY;
        try {
            nowTimeYMD = Long.parseLong(TimeUtils.dateToStamp2(TimeUtils.dateToString(TimeUtils.getTimeStame(), "yyyy-MM-dd")));
            nowTimeY = TimeUtils.dateToString(TimeUtils.getTimeStame(), "yyyy");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        //元旦节
        Solar sYuandan = Solar.fromYmd(lunar.getYear(), 1, 1);
        //腊八节
        Lunar lLaba = Lunar.fromYmd(lunar.getYear(), 12, 8);
        //北方小年
        Lunar lBxiaonian = Lunar.fromYmd(lunar.getYear(), 12, 23);
        //南方小年
        Lunar lNxiaonian = Lunar.fromYmd(lunar.getYear(), 12, 24);
        //除夕
        Lunar lChuxi = Lunar.fromYmd(lunar.getYear() + 1, 1, 1);
        //春节
        Lunar lChunjie;
        if (nowTimeY.equals(String.valueOf(lunar.getYear()))) {
            lChunjie = Lunar.fromYmd(lunar.getYear(), 1, 1);
        } else {
            lChunjie = Lunar.fromYmd(lunar.getYear() + 1, 1, 1);
        }
        //元宵节
        Lunar lYuanxiaojie = Lunar.fromYmd(lunar.getYear(), 1, 15);
        //情人节
        Solar sQingrenjie = Solar.fromYmd(lunar.getYear(), 2, 14);
        //龙抬头
        Lunar lLongtaitou = Lunar.fromYmd(lunar.getYear(), 2, 2);
        //妇女节
        Solar sFunvjie = Solar.fromYmd(lunar.getYear(), 3, 8);
        //劳动节
        Solar sLaodongjie = Solar.fromYmd(lunar.getYear(), 5, 1);
        //青年节
        Solar sQingnianjie = Solar.fromYmd(lunar.getYear(), 5, 4);
        //端午节
        Lunar lDuanwujie = Lunar.fromYmd(lunar.getYear(), 5, 5);
        //儿童节
        Solar sErtongjie = Solar.fromYmd(lunar.getYear(), 6, 1);
        //建党节
        Solar sJiandangjie = Solar.fromYmd(lunar.getYear(), 7, 1);
        //建军节
        Solar sJianjunjie = Solar.fromYmd(lunar.getYear(), 8, 1);
        //七夕节
        Lunar lQixijie = Lunar.fromYmd(lunar.getYear(), 7, 7);
        //中元节
        Lunar lZhongyuanjie = Lunar.fromYmd(lunar.getYear(), 7, 15);
        //教师节
        Solar sJiaoshijie = Solar.fromYmd(lunar.getYear(), 9, 10);
        //国庆节
        Solar sGuoqingjie = Solar.fromYmd(lunar.getYear(), 10, 1);
        //中秋节
        Lunar lZhongqiujie = Lunar.fromYmd(lunar.getYear(), 8, 15);
        //重阳节
        Lunar lChongyangjie = Lunar.fromYmd(lunar.getYear(), 9, 9);

        try {
            long yuandan = Long.parseLong(TimeUtils.dateToStamp(sYuandan + " 00:00:00"));
            long laba = Long.parseLong(TimeUtils.dateToStamp(lLaba.getSolar().toString() + " 00:00:00"));
            long bxiaonian = Long.parseLong(TimeUtils.dateToStamp(lBxiaonian.getSolar().toString() + " 00:00:00"));
            long nxiaonian = Long.parseLong(TimeUtils.dateToStamp(lNxiaonian.getSolar().toString() + " 00:00:00"));
            long chuxi = Long.parseLong(TimeUtils.dateToStamp(lChuxi.next(-1).getSolar().toString() + " 00:00:00"));
            chunjie = Long.parseLong(TimeUtils.dateToStamp(lChunjie.getSolar().toString() + " 00:00:00"));
            long yuanxiaojie = Long.parseLong(TimeUtils.dateToStamp(lYuanxiaojie.getSolar().toString() + " 00:00:00"));
            long qingrenjie = Long.parseLong(TimeUtils.dateToStamp(sQingrenjie + " 00:00:00"));
            long longtaitou = Long.parseLong(TimeUtils.dateToStamp(lLongtaitou.getSolar().toString() + " 00:00:00"));
            long funvjie = Long.parseLong(TimeUtils.dateToStamp(sFunvjie + " 00:00:00"));
            long laodongjie = Long.parseLong(TimeUtils.dateToStamp(sLaodongjie + " 00:00:00"));
            long qingnianjie = Long.parseLong(TimeUtils.dateToStamp(sQingnianjie + " 00:00:00"));
            long duanwujie = Long.parseLong(TimeUtils.dateToStamp(lDuanwujie.getSolar().toString() + " 00:00:00"));
            long ertongjie = Long.parseLong(TimeUtils.dateToStamp(sErtongjie + " 00:00:00"));
            long jiandangjie = Long.parseLong(TimeUtils.dateToStamp(sJiandangjie + " 00:00:00"));
            long jianjunjie = Long.parseLong(TimeUtils.dateToStamp(sJianjunjie + " 00:00:00"));
            long qixijie = Long.parseLong(TimeUtils.dateToStamp(lQixijie.getSolar().toString() + " 00:00:00"));
            long zhongyuanjie = Long.parseLong(TimeUtils.dateToStamp(lZhongyuanjie.getSolar().toString() + " 00:00:00"));
            long jiaoshijie = Long.parseLong(TimeUtils.dateToStamp(sJiaoshijie + " 00:00:00"));
            long guoqingjie = Long.parseLong(TimeUtils.dateToStamp(sGuoqingjie + " 00:00:00"));
            long zhongqiujie = Long.parseLong(TimeUtils.dateToStamp(lZhongqiujie.getSolar().toString() + " 00:00:00"));
            long chongyangjie = Long.parseLong(TimeUtils.dateToStamp(lChongyangjie.getSolar().toString() + " 00:00:00"));

            list.clear();

            Jieri jieri2 = new Jieri();
            jieri2.setId("yuandan");
            jieri2.setName("元旦节");
            jieri2.setTime(yuandan - nowTimeYMD);
            if (yuandan - nowTimeYMD >= 0) {
                list.add(jieri2);
            }

            Jieri jieri3 = new Jieri();
            jieri3.setId("laba");
            jieri3.setName("腊八节");
            jieri3.setTime(laba - nowTimeYMD);
            if (laba - nowTimeYMD >= 0) {
                list.add(jieri3);
            }

            Jieri jieri4 = new Jieri();
            jieri4.setId("bxiaonian");
            jieri4.setName("北方小年");
            jieri4.setTime(bxiaonian - nowTimeYMD);
            if (bxiaonian - nowTimeYMD >= 0) {
                list.add(jieri4);
            }

            Jieri jieri5 = new Jieri();
            jieri5.setId("nxiaonian");
            jieri5.setName("南方小年");
            jieri5.setTime(nxiaonian - nowTimeYMD);
            if (nxiaonian - nowTimeYMD >= 0) {
                list.add(jieri5);
            }

            Jieri jieri6 = new Jieri();
            jieri6.setId("chuxi");
            jieri6.setName("除夕");
            jieri6.setTime(chuxi - nowTimeYMD);
            if (chuxi - nowTimeYMD >= 0) {
                list.add(jieri6);
            }

            Jieri jieri7 = new Jieri();
            jieri7.setId("chunjie");
            jieri7.setName("春节");
            jieri7.setTime(chunjie - nowTimeYMD);
            if (chunjie - nowTimeYMD >= 0) {
                list.add(jieri7);
            }

            Jieri jieri8 = new Jieri();
            jieri8.setId("yuanxiaojie");
            jieri8.setName("元宵节");
            jieri8.setTime(yuanxiaojie - nowTimeYMD);
            if (yuanxiaojie - nowTimeYMD >= 0) {
                list.add(jieri8);
            }

            Jieri jieri9 = new Jieri();
            jieri9.setId("qingrenjie");
            jieri9.setName("情人节");
            jieri9.setTime(qingrenjie - nowTimeYMD);
            if (qingrenjie - nowTimeYMD >= 0) {
                list.add(jieri9);
            }

            Jieri jieri10 = new Jieri();
            jieri10.setId("longtaitou");
            jieri10.setName("龙抬头");
            jieri10.setTime(longtaitou - nowTimeYMD);
            if (longtaitou - nowTimeYMD >= 0) {
                list.add(jieri10);
            }

            Jieri jieri11 = new Jieri();
            jieri11.setId("funvjie");
            jieri11.setName("妇女节");
            jieri11.setTime(funvjie - nowTimeYMD);
            if (funvjie - nowTimeYMD >= 0) {
                list.add(jieri11);
            }

            Jieri jieri13 = new Jieri();
            jieri13.setId("laodongjie");
            jieri13.setName("劳动节");
            jieri13.setTime(laodongjie - nowTimeYMD);
            if (laodongjie - nowTimeYMD >= 0) {
                list.add(jieri13);
            }

            Jieri jieri14 = new Jieri();
            jieri14.setId("qingnianjie");
            jieri14.setName("青年节");
            jieri14.setTime(qingnianjie - nowTimeYMD);
            if (qingnianjie - nowTimeYMD >= 0) {
                list.add(jieri14);
            }

            Jieri jieri15 = new Jieri();
            jieri15.setId("duanwujie");
            jieri15.setName("端午节");
            jieri15.setTime(duanwujie - nowTimeYMD);
            if (duanwujie - nowTimeYMD >= 0) {
                list.add(jieri15);
            }

            Jieri jieri16 = new Jieri();
            jieri16.setId("ertongjie");
            jieri16.setName("儿童节");
            jieri16.setTime(ertongjie - nowTimeYMD);
            if (ertongjie - nowTimeYMD >= 0) {
                list.add(jieri16);
            }

            Jieri jieri17 = new Jieri();
            jieri17.setId("jiandangjie");
            jieri17.setName("建党节");
            jieri17.setTime(jiandangjie - nowTimeYMD);
            if (jiandangjie - nowTimeYMD >= 0) {
                list.add(jieri17);
            }

            Jieri jieri18 = new Jieri();
            jieri18.setId("jianjunjie");
            jieri18.setName("建军节");
            jieri18.setTime(jianjunjie - nowTimeYMD);
            if (jianjunjie - nowTimeYMD >= 0) {
                list.add(jieri18);
            }

            Jieri jieri19 = new Jieri();
            jieri19.setId("qixijie");
            jieri19.setName("七夕节");
            jieri19.setTime(qixijie - nowTimeYMD);
            if (qixijie - nowTimeYMD >= 0) {
                list.add(jieri19);
            }

            Jieri jieri20 = new Jieri();
            jieri20.setId("zhongyuanjie");
            jieri20.setName("中元节");
            jieri20.setTime(zhongyuanjie - nowTimeYMD);
            if (zhongyuanjie - nowTimeYMD >= 0) {
                list.add(jieri20);
            }

            Jieri jieri21 = new Jieri();
            jieri21.setId("jiaoshijie");
            jieri21.setName("教师节");
            jieri21.setTime(jiaoshijie - nowTimeYMD);
            if (jiaoshijie - nowTimeYMD >= 0) {
                list.add(jieri21);
            }

            Jieri jieri22 = new Jieri();
            jieri22.setId("guoqingjie");
            jieri22.setName("国庆节");
            jieri22.setTime(guoqingjie - nowTimeYMD);
            if (guoqingjie - nowTimeYMD >= 0) {
                list.add(jieri22);
            }

            Jieri jieri23 = new Jieri();
            jieri23.setId("zhongqiujie");
            jieri23.setName("中秋节");
            jieri23.setTime(zhongqiujie - nowTimeYMD);
            if (zhongqiujie - nowTimeYMD >= 0) {
                list.add(jieri23);
            }

            Jieri jieri24 = new Jieri();
            jieri24.setId("chongyangjie");
            jieri24.setName("重阳节");
            jieri24.setTime(chongyangjie - nowTimeYMD);
            if (chongyangjie - nowTimeYMD >= 0) {
                list.add(jieri24);
            }

            // 使用Collections.sort()方法进行升序排序
            Collections.sort(list, (p1, p2) -> Long.compare(p1.getTime(), p2.getTime()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}